package com.onenine.app.activity;

import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.view.animation.AnimationUtils;

import com.onenine.app.R;
import com.onenine.app.application.NineteenApp;
import com.onenine.base.base.BaseActivity;
import com.onenine.base.utils.StatusBarUtils;
import com.onenine.chat.rongcloud.RcUser;
import com.onenine.common.util.ActivityUtils;

import java.util.List;

import pub.devrel.easypermissions.AppSettingsDialog;
import pub.devrel.easypermissions.EasyPermissions;

/**
 * 创建时间：2018/5/8 0008 14:27
 * 创建人：fly
 * 功能描述：App启动页
 */
public class SplashActivity extends BaseActivity implements EasyPermissions.PermissionCallbacks {

    private final int RC_PERMISSION = 0x0001;
    private final String[] PERMISSIONS = {Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_PHONE_STATE
            , Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION};
    private View mvLogo;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        StatusBarUtils.setTransparent(this);
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void initViews() {
        super.initViews();
        mvLogo = findViewById(R.id.iv_logo);
        mvLogo.startAnimation(AnimationUtils
                .loadAnimation(this, R.anim.anim_splash_logo_fade_in));
    }

    @Override
    protected void initData() {
        //检查相应权限
        if (EasyPermissions.hasPermissions(this, PERMISSIONS)) {
            launchNextActivity(true);
        } else {
            EasyPermissions.requestPermissions(this
                    , getString(R.string.permission_extra_storage_state_hint)
                    , RC_PERMISSION, PERMISSIONS);
        }
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_splash;
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
                                           @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
    }

    @Override
    public void onPermissionsGranted(int requestCode, List<String> perms) {
        if (requestCode == RC_PERMISSION && perms.size() == PERMISSIONS.length) {
            launchNextActivity(false);
        }
    }

    @Override
    public void onPermissionsDenied(int requestCode, List<String> perms) {
        launchNextActivity(false);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == AppSettingsDialog.DEFAULT_SETTINGS_REQ_CODE) {
            launchNextActivity(false);
        }
    }

    /**
     * 加载下一步Activity
     */
    private void launchNextActivity(boolean delay) {
        NineteenApp.sHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
//                if (!SpCommonHelper.getInstance().isGuide()) {
//                    ActivityUtils.startActivity(SplashActivity.this, GuideActivity.class);
//                } else
                if (TextUtils.isEmpty(RcUser.getInstance().getUserId())) {
                    ActivityUtils.startActivity(SplashActivity.this, LoginActivity.class);
                } else {
                    ActivityUtils.startActivity(SplashActivity.this, MainActivity.class);
                }
                finish();
            }
        }, delay ? getResources()
                .getInteger(R.integer.splash_anim_duration) : 0);

    }

}
